// pages/shopManager/goodsManager/goodsManager/goodsManager.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsClassify: [],
        classifyId: null,
        goodsType: null,
        goodsStatus: 1,
        goodsList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.classifyId) {
            this.setData({
                classifyId: options.classifyId,
                goodsType: options.type
            })
        } else {
            this.setData({
                goodsType: options.type
            })
        }
        this.getGoodsList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getGoodsClassify()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        this.setData({
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getGoodsList()
    },

    getGoodsClassify: function() {
        const that = this
        let data = {
            store_id: wx.getStorageSync('storeId'),
            page: 0,
            limit: 10
        }
        common.post('Goodsclass/store_class_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            }
            that.setData({
                goodsClassify: res
            })
        }).catch(function(e) {})
    },

    getGoodsList: function() {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let goodsList = this.data.goodsList
        let data
        if (this.data.classifyId == null) {
            if (this.data.goodsType == 'normal') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '0',
                    client: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.goodsType == 'vip') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    client: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            } else if (this.data.goodsType == 'agent') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    agency_goods: '1',
                    is_goods: '1',
                    client: 2,
                    limit: 10,
                    page: this.data.nowPage
                }
            }
        } else {
            data = {
                class_id: this.data.classifyId,
                store_id: wx.getStorageSync('storeId'),
                is_sale: this.data.goodsStatus,
                is_goods: '0',
                client: 2,
                limit: 10,
                page: this.data.nowPage
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goods/goods_list', data).then(function(res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: goodsList,
                    isAll: true
                })
            } else {
                goodsList = goodsList.concat(res)
                that.setData({
                    goodsList: goodsList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function(e) {})
    },

    navClick: function(e) {
        if (this.data.goodsStatus == e.currentTarget.dataset.status) {
            return
        }
        this.setData({
            goodsStatus: e.currentTarget.dataset.status,
            goodsList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        this.getGoodsList()
    },

    goClassifyManager: function() {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/classifyManager/classifyManager'
        })
    },

    goBatchManager: function(e) {
        if (this.data.classifyId == null) {
            wx.navigateTo({
                url: '/pages/shopManager/goodsManager/batchManager/batchManager?type=' + e.currentTarget.dataset.type + '&goodsType=' + this.data.goodsType
            })
        } else {
            wx.navigateTo({
                url: '/pages/shopManager/goodsManager/batchManager/batchManager?type=' + e.currentTarget.dataset.type + '&goodsType=' + this.data.goodsType + '&classifyId=' + this.data.classifyId
            })
        }
    },

    goAddGoods: function() {
        if (this.data.goodsClassify.length == 0) {
            wx.showModal({
                title: '提示',
                content: '您还没有添加商品分类，请前往添加商品分类',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/shopManager/goodsManager/classifyManager/classifyManager'
                        })
                    }
                }
            })
        } else {
            wx.navigateTo({
                url: '/pages/shopManager/goodsManager/goodsEdit/goodsEdit?type=' + this.data.goodsType,
            })
        }
    },

    goodsEdit: function(e) {
        wx.navigateTo({
            url: '/pages/shopManager/goodsManager/goodsEdit/goodsEdit?type=' + this.data.goodsType + '&goodsId=' + e.currentTarget.dataset.id,
        })
    },

    updateData: function() {
        const that = this
        let data
        if (this.data.classifyId == null) {
            if (this.data.goodsType == 'normal') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '0',
                    client: 2,
                    limit: this.data.nowPage * 10,
                    page: 0
                }
            } else if (this.data.goodsType == 'vip') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    is_goods: '1',
                    client: 2,
                    limit: this.data.nowPage * 10,
                    page: 0
                }
            } else if (this.data.goodsType == 'agent') {
                data = {
                    store_id: wx.getStorageSync('storeId'),
                    is_sale: this.data.goodsStatus,
                    agency_goods: '1',
                    is_goods: '1',
                    client: 2,
                    limit: this.data.nowPage * 10,
                    page: 0
                }
            }
        } else {
            data = {
                class_id: this.data.classifyId,
                store_id: wx.getStorageSync('storeId'),
                is_sale: this.data.goodsStatus,
                is_goods: '0',
                client: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Goods/goods_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    goodsList: []
                })
            } else {
                that.setData({
                    goodsList: res
                })
            }
        }).catch(function(e) {})
    }
})